import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_ncsjqz_getx/util/regexp_util.dart';
import 'package:flutter_ncsjqz_getx/util/toast_util.dart';
import 'package:flutter_ncsjqz_getx/widget/app_bar_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/base_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/gradient_button.dart';
import 'package:flutter_ncsjqz_getx/widget/ink_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/row_line_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/tab_widget.dart';
import 'package:get/get.dart';

import 'money_logic.dart';

/// <summary>
/// todo: 我的钱包
/// author：zwb
/// dateTime： 2021/11/9 14:18
/// filePath： lib/page/my/money/money_view.dart
/// desc:
/// <summary>
///
class MoneyPage extends StatefulWidget {
  @override
  _MoneyPageState createState() => _MoneyPageState();
}

class _MoneyPageState extends State<MoneyPage> {
  final logic = Get.put(MoneyLogic());

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    logic.getData();
    logic.wxEvent();
  }
  @override
  Widget build(BuildContext context) {
    return GetBuilder<MoneyLogic>(
      builder: (controller) {
        return Scaffold(
          appBar: appBarWidget(title: "我的钱包"),
          body: baseScaffoldWidget(
            onRefresh: () async{
              logic.getData();
            },
            child: Padding(
              child: Column(
                children: [
                  _moneyCard(),
                  _tab(),
                  _dataList(),
                ],
              ),
              padding: EdgeInsets.all(15),
            ),
          ),
          bottomNavigationBar: Container(
            width: Get.width,
            height: 80,
            child: Container(
              width: Get.width * 0.7,
              padding: EdgeInsets.symmetric(vertical: 15,horizontal: Get.width / 4.5),
              child: OutlineButton(
                onPressed: (){
                  _bottomMoneyPay();
                },
                child: InkWidget.title("充 值",color: Colors.green),
                shape: StadiumBorder(),
                borderSide: BorderSide(color: Colors.green),
              ),
            ),
          ),
        );
      },
    );
  }

  /// todo：tab菜单
  _tab(){
    return tab(
      tabs: ["明细"],
      currentIndex: 0,
      padding: EdgeInsets.symmetric(vertical: 10),
      onTap: (v){}
    );
  }

  /// todo：列表item
  _item(index){
    return Container(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              InkWidget.imgAsset("money.png",width: 25,),
              SizedBox(width: 10,),
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  InkWidget.title("${logic.xList![index].typeName}"),
                  InkWidget.text("${logic.xList![index].operationDate}",fonSize: 12,color: Colors.grey),
                ],
              ),
            ],
          ),
          SizedBox(width: 10,),
          Expanded(child: Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              InkWidget.title("${logic.xList![index].amount}",color: Colors.green),
              InkWidget.text("${logic.xList![index].transactionNo ?? ''}",fonSize: 12,color: Colors.grey),
            ],
          ),)
        ],
      ),
      padding: EdgeInsets.symmetric(vertical: 15),
      decoration: BoxDecoration(
          border: Border(bottom: BorderSide(color: Colors.grey.shade200))
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<MoneyLogic>();
    logic.closeWxEvent();
    super.dispose();
  }

  /// todo：钱包余额卡片
  _moneyCard() {
    return Stack(
      children: [
        ClipRRect(
          borderRadius: BorderRadius.circular(10),
          child: InkWidget.imgAsset("money_bg.png",height:  120,width: Get.width,fit: BoxFit.cover,),
        ),
        Positioned(
          top: 120 / 3,
          left: 20,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              InkWidget.text("我的钱包",color: Colors.white),
              Row(
                children: [
                  InkWidget.title("${logic.totalMoney}",fonSize: 18,color: Colors.white),
                  InkWidget.text("元",fonSize: 12,color: Colors.white),
                ],
              ),
            ],
          ),
        ),
      ],
    );
  }

  /// todo：列表数据
  _dataList() {
    return ListView(
      shrinkWrap: true,
      physics: NeverScrollableScrollPhysics(),
      children: List.generate(logic.xList!.length, (index){
        return _item(index);
      }),
    );
  }

  /// todo：金额充值
  _bottomMoneyPay(){
    Get.bottomSheet(
      GetBuilder<MoneyLogic>(
        builder: (controller) {
          List payType = ["微信支付","支付宝支付"];
          List payIcon = ["wx.png","zfb.png","zfb.png"];
          double height = payType.length < 3 ?  280 : 345;
          return SingleChildScrollView(
            physics: NeverScrollableScrollPhysics(),
            child: Material(
              child: Container(
                height: height,
                color: Colors.white,
                padding: EdgeInsets.all(15),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    /// todo：标题
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        InkWidget.imgAsset("rem.png",width: 25,color: Colors.white),
                        InkWidget.text("充值金额",fonSize: 16),
                        InkWidget.imgAsset("rem.png",width: 25,onTap: (){
                          Get.back();
                        }),
                      ],
                    ),
                    /// todo： 充值输入框
                    SizedBox(
                      height: 40,
                      child: TextField(
                        textAlign: TextAlign.center,
                        keyboardType: TextInputType.number,
                        controller: logic.textEditingControllerMoney,
                        inputFormatters: [
                          FilteringTextInputFormatter.allow(RegExps.num09Exp),
                        ],
                        decoration: InputDecoration(
                            hintText: "请输入充值金额(整数)",
                            focusedBorder: UnderlineInputBorder(
                                borderSide: BorderSide(color: Colors.green)
                            )
                        ),
                      ),
                    ),
                    InkWidget.title("\n选择支付方式"),
                    /// todo：支付方式
                    Expanded(
                      child: ListView(
                        shrinkWrap: true,
                        children: List.generate(payType.length, (index){
                          return rowBaseWidget(
                              left: Row(
                                children: [
                                  InkWidget.imgAsset("${payIcon[index]}",width: 22,fit: BoxFit.fill),
                                  InkWidget.text(" ${payType[index]}"),
                                ],
                              ),
                              height: 45,
                              right:  InkWidget.imgAsset("select.png",width: 20,color: logic.payType != index ? Colors.white:null),
                              padding: EdgeInsets.all(0),
                              onTap: (){
                                logic.setPayType(index);
                              }
                          );
                        }),
                      ),
                    ),
                    Center(
                      child: SizedBox(
                        width: Get.width * 0.7,
                        child: gradientButton("确 定",onTap: (){
                          print("${payType[logic.payType]},${logic.textEditingControllerMoney.value.text}元");
                          if(logic.textEditingControllerMoney.value.text == "") return toastCenter("请输入充值金额");
                          if(logic.textEditingControllerMoney.value.text.startsWith("0")) return toastCenter("金额不能以0开头");
                          logic.recharge();
                        },circular: 40),
                      ),
                    ),
                  ],
                ),
              ),
              color: Colors.transparent,
            ),
          );
        },
      ),
      isDismissible: false,
      enableDrag: false,
      isScrollControlled: true,
    ).then((value) {
      logic.setPayType(0);
      logic.textEditingControllerMoney.text = "";
    });
  }
}